package com.example.auv_660.auvorderload;

import com.example.auv_660.MainActivity;
import com.example.auv_660.autocontrolalgorithm.assistantsensor.BatterySensor;
import com.example.auv_660.autocontrolalgorithm.assistantsensor.LocationSensor;
import com.example.auv_660.autocontrolalgorithm.assistantsensor.OrientationSensor;
import com.example.auv_660.network.udp.HFUDPBuild;
import com.example.auv_660.util.Constants;
import com.example.auv_660.util.MsgType;
import com.example.auv_660.util.Tools;

import java.util.Date;

public class AUVModelStateCheckOrderLoad extends OrderLoad {
    private OrientationSensor orientationSensor;
    private MainActivity mainActivity = MainActivity.getInstance();
    private LocationSensor locationSensor;
    private BatterySensor batterySensor;

    @Override
    public void doOrder(String order) {
        if (order.contains(Constants.CHECK_AUV_MODEL_STATE_ORDER))
        {
            mainActivity.sendMsgToMainThread(MsgType.CONTROLLER_MSG, "查询状态");

            Constants.ORDER = false;

            orientationSensor = new OrientationSensor();

            if (mainActivity != null)
            {
                mainActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        locationSensor = new LocationSensor();
                    }
                });
            }

            batterySensor = new BatterySensor();
            try
            {
                Thread.sleep(10000);
            }
            catch (InterruptedException e)
            {
                e.printStackTrace();
            }

            double[] locationValues = locationSensor.getValue("NORMAL");
            float[] orientationValues = orientationSensor.getValue();

            System.out.println("0: " + orientationValues[0] + " 1: " + orientationValues[1] + " 2: " + orientationValues[2]);
            String answer = Constants.CHECK_AUV_MODEL_STATE_ANSWER + Constants.PHONEIMEI + "," +
                    Tools.DateFormat(new Date(), "s") + "," + locationValues[0] + "," + locationValues[1]
                    + "," + locationValues[2] + "," + "0" + "," + orientationValues[0] + "," + orientationValues[1]
                    + "," + orientationValues[2] + "," + batterySensor.getCurrentBattery() + "," + "0" + "," + "0" + "," + "0";
            HFUDPBuild.INSTANCE.getHFUdpClient().sendMessage("C" + answer);

            orientationSensor.unregister();
            locationSensor.stopLocation();
            batterySensor.releaseSource();

            Constants.ORDER = true;

            return;
        }
        else
        {
            OrderLoad nexOrder = this.getNextOrder();
            if (nexOrder != null)
            {
                nexOrder.doOrder(order);
            }
        }
    }
}
